package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.VoucherStockDetail;
import com.turing.post.organization.util.VoucherStockDetailBusiness;
import com.turing.post.organization.util.VoucherStockDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherStockDetailBusinessImpl")
@Remote
@Local
public class VoucherStockDetailBusinessImpl implements
		VoucherStockDetailBusiness {

	@EJB(mappedName = "organization.impl.VoucherStockDetailDaoImpl")
	private VoucherStockDetailDao voucherStockDetailDaoImpl;// 凭证出入库明细表

	// @EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	// private PCPSCommonDao pcpsCommonDaoImpl;//主键

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;// 日志

	/**
	 * 新增凭证出入库明细表
	 * 
	 * @param VoucherStockDetail
	 *            凭证出入库明细表
	 * @throws PCPSApplicationException
	 */
	public void insertVoucherStockDetail(PCPSWebInput webInput,
			VoucherStockDetail voucherStockDetail)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 操作员
		if (webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"操作员为空!");
		}
		// 新增
		voucherStockDetailDaoImpl.insertVoucherStockDetail(voucherStockDetail);
		insertRecordingOperationLog(webInput, "凭证出入库明细新增", voucherStockDetail,
				SysOperLog.operType_Edit, "StartVoucherNo:"
						+ voucherStockDetail.getStartVoucherNo()
						+ "EndVoucherNo:"
						+ voucherStockDetail.getEndVoucherNo());
	}

	/**
	 * 删除凭证出入库明细表
	 * 
	 * @param VoucherStockDetail
	 *            凭证出入库明细表
	 * @throws PCPSApplicationException
	 */
	public void deleteVoucherStockDetail(PCPSWebInput webInput,
			VoucherStockDetail voucherStockDetail)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 操作员
		if (webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"操作员为空!");
		}
		// 操作员
		if (voucherStockDetail == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证出入库明细为空!");
		}

		insertRecordingOperationLog(webInput, "凭证出入库明细删除", voucherStockDetail,
				SysOperLog.operType_Edit, "StartVoucherNo:"
						+ voucherStockDetail.getStartVoucherNo()
						+ "EndVoucherNo:"
						+ voucherStockDetail.getEndVoucherNo());
		voucherStockDetailDaoImpl.deleteVoucherStockDetail(voucherStockDetail);

	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, VoucherStockDetail obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
